home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1834 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. Path: dynamic15.interworld.com.au!user
  2. From: patersr@iinet.com.au (Robert Paterson)
  3. Newsgroups: comp.lang.c++,comp.sys.mac.programmer.help,comp.sys.mac.oop.misc,comp.sys.mac.oop.tcl
  4. Subject: Re: [Q] Overloading operator ->
  5. Date: Sat, 13 Jan 1996 13:13:18 +0800
  6. Organization: FlexiPlan Australia Ltd
  7. Message-ID: <patersr-1301961313180001@dynamic15.interworld.com.au>
  8. References: <mlj-0901960854360001@urals.jpl.nasa.gov>
  9. NNTP-Posting-Host: dynamic15.interworld.com.au
  10.  
  11. In article <mlj-0901960854360001@urals.jpl.nasa.gov>,
  12. mlj@tazboy.jpl.nasa.gov (Mose L. Johnson) wrote:
  13.  
  14. > I am making a small interface for debuging prposes.  The set of classes should
  15. > allow the source to remain as close to its original form as possible.  One
  16. > of the things I am working on is Memory Management.
  17. > One of the things I want to do is overload the arrow(->) operator.  Here
  18. > is an example of what I tried and want to do.
  19. > ---
  20. > template <class T> class ref<class T>
  21. > {
  22. >    public:
  23. >    T* operator ->();
  24. > };
  25. > class tester
  26. > {
  27. >    public:
  28. >    void member();
  29. > };
  30. > void main()
  31. > {
  32. >    ref<tester *> var;
  33. >    var->member();
  34. > }
  35. > ---
  36. > When I try to compile this, my compiler gives me an error of:
  37. >    illegal return type for operator->()
  38.  
  39.  
  40. I think it's because you delcare ref<tester *> var instead of ref<tester> var.
  41.  
  42. "The operator->() function must return a pointer to an object of the class that
  43. operator->() operates upon." C++ The Complete Reference by Herbert Schildt, 
  44. p380.
  45.  
  46. Because you declared ref<tester *> you are returning a pointer to a
  47. pointer to the class that operator->() operates upon. If you declare
  48. ref<tester> the 
  49. operator will work, but may no longer reflect what you are trying to do.
  50.  
  51. Hope this helps,
  52.  
  53. Regards,
  54.  
  55. Robert Paterson.
  56.